package com.ai.notices.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.ai.notices.common.util.BeanUtil;
import com.ai.notices.common.util.PagedResult;
import com.ai.notices.dao.mysql.mapper.IdentityCardImpMapper;
import com.ai.notices.dao.mysql.mapper.IdentityCardMapper;
import com.ai.notices.dao.mysql.model.AuthStaff;
import com.ai.notices.dao.mysql.model.IdentityCard;
import com.ai.notices.dao.mysql.model.IdentityCardExample;
import com.ai.notices.dao.mysql.model.IdentityCardImp;
import com.ai.notices.dao.mysql.model.IdentityCardImpExample;
import com.ai.notices.service.interfaces.IIdentityCardSV;
import com.ai.notices.web.FileUtils;
import com.github.pagehelper.PageHelper;

/**
 * Created by huangzh on 17/4/13.
 */
@Service
public class IdentityCardSVImpl implements IIdentityCardSV {

    @Autowired
    private IdentityCardImpMapper identityCardImpMapper;

    @Autowired
    private IdentityCardMapper identityCardMapper;

	@Override
	public PagedResult<IdentityCard> queryIdentityCardByPage(IdentityCard identityCard, Integer pageNo, Integer pageSize)
			throws Exception {
		// TODO Auto-generated method stub
		pageNo = pageNo == null ? 1 : pageNo;
		pageSize = pageSize == null ? 10 : pageSize;
		PageHelper.startPage(pageNo, pageSize); // startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
		IdentityCardExample example = new IdentityCardExample();
		IdentityCardExample.Criteria criteria = example.createCriteria();
		criteria.andStateEqualTo("1");
		if (!StringUtils.isEmpty(identityCard.getIdentityCard())) {
			criteria.andIdentityCardLike("%" + identityCard.getIdentityCard() + "%");
		}
		if (!StringUtils.isEmpty(identityCard.getName())) {
			criteria.andNameLike("%" + identityCard.getName() + "%");
		}
		return BeanUtil.toPagedResult(identityCardMapper.selectByExample(example));
	}
	
	@Override
	public void saveIdentityCardImp(List<Map<String, String>> identityCardLst, String batchId, AuthStaff authStaff)
			throws Exception {
		Date date = new Date();
		for (Map<String, String> map : identityCardLst) {
			IdentityCardImp imp = new IdentityCardImp();
			imp.setBatchId(batchId);
			imp.setIdentityCard(map.get("0"));
			imp.setName(map.get("1"));
			imp.setCreateStaff(authStaff.getStaffCode());
			imp.setCreateTime(date);
			imp.setState("0");
			identityCardImpMapper.insert(imp);
		}
	}
	
	@Override
	public void saveIdentityCard(String batchId, AuthStaff authStaff)
			throws Exception {
		Date date = new Date();
		IdentityCardImpExample identityCardImpExample = new IdentityCardImpExample();
		identityCardImpExample.createCriteria().andBatchIdEqualTo(batchId).andStateEqualTo("0");
		List<IdentityCardImp> identityCardImpLst = identityCardImpMapper.selectByExample(identityCardImpExample);
		for (IdentityCardImp imp : identityCardImpLst) {
			if (StringUtils.isBlank(imp.getIdentityCard()) || StringUtils.isBlank(imp.getName())) {
				imp.setState("2");
				imp.setErrDesc("有字段为空");
				identityCardImpMapper.updateByPrimaryKey(imp);
				continue;
			}
			IdentityCardExample identityCardExample = new IdentityCardExample();
			identityCardExample.createCriteria().andIdentityCardEqualTo(imp.getIdentityCard());
			List<IdentityCard> identityCardLst = identityCardMapper.selectByExample(identityCardExample);
			if (!CollectionUtils.isEmpty(identityCardLst)) {
				if (StringUtils.equals(identityCardLst.get(0).getName(), imp.getName())) {
					imp.setState("2");
					imp.setErrDesc("此身份证已存在");
					identityCardImpMapper.updateByPrimaryKey(imp);
					continue;
				} else {
					imp.setState("2");
					imp.setErrDesc("身份证号码和人名对不上");
					identityCardImpMapper.updateByPrimaryKey(imp);
					continue;
				}
			}
			IdentityCard identityCard = new IdentityCard();
			identityCard.setBatchId(imp.getBatchId());
			identityCard.setIdentityCard(imp.getIdentityCard());
			identityCard.setName(imp.getName());
			identityCard.setCreateStaff(authStaff.getStaffCode());
			identityCard.setCreateTime(date);
			identityCard.setState("1");
			String code = FileUtils.getIdCard(identityCard);
			if (!"true".equals(code)) {
				imp.setState("2");
				imp.setErrDesc("身份证验证不通过");
				if ("error".equals(code)) {
					imp.setErrDesc("调用身份证验证接口报错");
				}
				identityCardImpMapper.updateByPrimaryKey(imp);
				continue;
			}
			identityCardMapper.insert(identityCard);
			imp.setState("1");
			identityCardImpMapper.updateByPrimaryKey(imp);
		}
	}

	@Override
	public PagedResult<IdentityCardImp> queryIdentityCardImpByPage(IdentityCardImp imp, Integer pageNo, Integer pageSize)
			throws Exception {
		// TODO Auto-generated method stub
		pageNo = pageNo == null ? 1 : pageNo;
		pageSize = pageSize == null ? 10 : pageSize;
		PageHelper.startPage(pageNo, pageSize); // startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
		IdentityCardImpExample example = new IdentityCardImpExample();
		IdentityCardImpExample.Criteria criteria = example.createCriteria();		
		criteria.andBatchIdEqualTo(imp.getBatchId());
		if (StringUtils.isNotBlank(imp.getState())) {
			criteria.andStateEqualTo(imp.getState());
		}
		return BeanUtil.toPagedResult(identityCardImpMapper.selectByExample(example));
	}
}
